package handler

import (
	"cargo-master/cache"
	"cargo-master/model"
	"cargo-master/pkg"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "不能为空",
		})
		return
	}
	rediskey := "login_" + username
	cachedata, _ := cache.Rdb.Get(cache.Ctx, rediskey).Int()
	if cachedata == 3 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "密码错误3次，封禁二小时",
		})
		return
	}
	var user model.User
	model.DB.Where("username =?", username).Find(&user)
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, rediskey)
		if cachedata == 2 {
			cache.Rdb.Set(cache.Ctx, rediskey, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 400,
				"msg":  "密码错误3次",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登陆成功",
		"user": user,
	})
	return
}
func UserRole(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := model.UserRole{
		Uid: uid,
		Rid: rid,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户和角色关联失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "用户和角色关联成功",
	})
	return
}
func RoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "角色与权限关联失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "角色与权限关联成功",
	})
	return

}
func UserRoleList(c *gin.Context) {
	uid := c.PostForm("uid")
	//SELECT
	//*
	//	FROM
	//roles
	//LEFT JOIN user_roles ON user_roles.rid=roles.id
	//WHERE user_roles.uid=1
	var role []model.Role
	model.DB.Table("roles").Joins("LEFT JOIN user_roles ON user_roles.rid=roles.id").
		Where("user_roles.uid =?", uid).Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": role,
	})
	return
}
func RoleNodeList(c *gin.Context) {
	rid := c.PostForm("rid")
	//SELECT
	//*
	//	FROM
	//nodes
	//LEFT JOIN role_nodes ON role_nodes.nid=nodes.id
	//WHERE role_nodes.rid=1
	var node []model.Node
	model.DB.Table("nodes").Joins("LEFT JOIN role_nodes ON role_nodes.nid=nodes.id").
		Where("role_nodes.rid =?", rid).Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
	return
}
func UserList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var user []model.User
	rediskey := "redis_list_" + strconv.Itoa(page)
	cachedata, _ := cache.Rdb.Get(cache.Ctx, rediskey).Result()
	if cachedata == "" {
		model.DB.Offset(offset).Limit(size).Find(&user)
		jsondata, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, rediskey, jsondata, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cachedata), &user)
	}
	var total int64
	model.DB.Model(&model.User{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  user,
		"total": total,
	})
	return
}
func UserDetail(c *gin.Context) {
	id := c.PostForm("id")
	var user model.User
	rediskey := "redis_detail_" + id
	cachedata, _ := cache.Rdb.Get(cache.Ctx, rediskey).Result()
	if cachedata == "" {
		model.DB.Where("id = ?", id).Find(&user)
		jsondata, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, rediskey, jsondata, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cachedata), &user)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return

}
